package com.zhc.dejingapi.service.upload.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhc.dejingapi.entity.QingEquipmentEntity;
import com.zhc.dejingapi.mapper.*;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.service.upload.IEQCurrentStatusTimeUploadService;
import com.zhc.dejingapi.utils.equipment.EquipmentCalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EQCurrentStatusTimeUploadServiceImpl implements IEQCurrentStatusTimeUploadService {


    private final JdbcTemplate mysqlJdbcTemplate;
    @Autowired
    QingflowApiInfo qqingflowApiInfo;

    @Autowired
    QingEquipmentMapper qingEquipmentMapper;

    @Autowired
    RealtimeMapper realtimeMapper;

    @Autowired
    SpcMapper spcMapper;

    @Autowired
    QingSfcmoroutingshiftMapper qingSfcmoroutingshiftMapper;

    //系统能用对象mapper

    //region 系统能用对象mapper
    @Autowired
    private QingAppfieldsMapper qingAppfieldsMapper;
    @Autowired
    private QingAppinfoMapper qingAppinfoMapper;

    @Autowired
    private QingAppfieldtablevaluesMapper qingAppfieldtablevaluesMapper;
    //endregion

    private final RedisTemplate<String,Object> redisTemplate;



    public EQCurrentStatusTimeUploadServiceImpl(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                               @Qualifier("redisTemplateWin") RedisTemplate<String, Object> redisTemplate) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Boolean UploadEQCurrentDueTime() {

        Boolean bSuccess=false;
        EquipmentCalUtils equipmentCalUtils = new EquipmentCalUtils();
        try{
            EquipmentCalUtils.LoadUploadUtis(qqingflowApiInfo,qingAppfieldsMapper,
                    qingAppfieldtablevaluesMapper,mysqlJdbcTemplate);

        }
        catch (Exception e){
            e.printStackTrace();
            log.error("上传设备当前时间失败"+e.getMessage()+"\r\n"+e.getStackTrace());
            bSuccess=false;
            return bSuccess;
        }

        if(equipmentCalUtils.eqCurrentStatusUploadUtils==null){
            return false;
        }

        QueryWrapper<QingEquipmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eqClass","注塑机");

        List<QingEquipmentEntity> listEq=qingEquipmentMapper.selectList(queryWrapper);
        if(listEq==null||listEq.isEmpty()){

        }
        else{
            for(QingEquipmentEntity entity:listEq){

                try{
                    equipmentCalUtils.CalAvailTime(qqingflowApiInfo,
                            realtimeMapper,spcMapper,
                            qingSfcmoroutingshiftMapper,
                            mysqlJdbcTemplate,entity);
                }
                catch (Exception e){
                    e.printStackTrace();
                    log.error("上传设备当前时间失败"+e.getMessage()+"\r\n"+e.getStackTrace());

                }


               // bSuccess=equipmentCalUtils.UploadToQing(qqingflowApiInfo,entity,mysqlJdbcTemplate);


            }
        }





        return bSuccess;
    }

}
